package com.woniu.controller;

import com.alibaba.fastjson.JSONObject;
import com.woniu.bo.RequestMessage;
import com.woniu.util.ResponseEnum;
import com.woniu.util.ResponseUtil;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @Classname CustomerServiceController
 * @Description TODO
 * @Date 2025/10/28 11:34
 * @Created by pc
 */
@RestController
@RequestMapping("/chatai")
public class CustomerServiceController {

    @Resource
    private RestTemplate restTemplate;

    //远程调用的方式   RestTemplate
    @PostMapping("/message2")
    public ResponseUtil receiveMessage2 (@RequestBody RequestMessage requestMessage) {
//       请求体数据
        Map<String, Object> requestBody = new HashMap<>();
        requestBody.put("message",  requestMessage.getMessage());
        requestBody.put("mode", "chat");
        requestBody.put("userId", requestMessage.getUserId());
//  请求头数据
        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.setContentType(MediaType.APPLICATION_JSON);
        requestHeaders.set("Authorization","Bearer 6GQ3MRN-54TM524-K0N1GTN-5J6TM5P");  // AIkey
        requestHeaders.set("accept", "application/json");
        HttpEntity<Map<String, Object>> r = new HttpEntity<Map<String, Object>>(requestBody, requestHeaders);

        String url = "http://localhost:3001/api/v1/workspace/demo/thread/7bcf6d74-3c37-4581-bba9-ca6787404349/chat";
        String content = restTemplate.postForObject(url, r, String.class);
        JSONObject jsonObject = JSONObject.parseObject(content);
        String aiAnswer = (String)jsonObject.get("textResponse");
        content = aiAnswer.split("</think>")[1];
        return  ResponseUtil.get(ResponseEnum.OK,content);
    }
}
